package com.chat.datea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.chat.datea.R;
import com.chat.datea.bean.SearchTopEntity;

import java.util.List;

/**
 * Created by Shura on 15/11/5.
 */
public class SearchTopGridViewAdapter extends BaseAdapter {

    private List<SearchTopEntity.ResultEntity> mResultEntities;
    private Context mContext;

    public SearchTopGridViewAdapter(Context context, List<SearchTopEntity.ResultEntity> resultEntities) {
        this.mContext = context;
        this.mResultEntities = resultEntities;
    }

    @Override
    public int getCount() {
//        return mResultEntities != null && mResultEntities.size() > 0 ? mResultEntities.size() : 0;
        return 9;
    }

    @Override
    public Object getItem(int position) {
        return mResultEntities != null && mResultEntities.size() > 0 ? mResultEntities.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            // 初始化缓存组件
            viewHolder = new ViewHolder();
            // 通过布局加载器加载行布局
            convertView = LayoutInflater.from(mContext).inflate(R.layout.search_top_gridview_item, null);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.search_tip);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            viewHolder.textView.setText(mResultEntities.get(position).getWord());
        } catch (IndexOutOfBoundsException e) {
            Toast.makeText(mContext, "已经没记录喽", Toast.LENGTH_SHORT).show();

        }

        return convertView;
    }

    public class ViewHolder {
        public TextView textView;
    }

}
